package tree;

import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of a node in a tree. Since this will be used for
 * pathfinding, nodes contain data for x, y position and whether the node
 * is walkable or not. Also contains references to its parent and its children
 * so we can traverse the tree later on.
 * 
 * Implements Comparable so that it can be sorted.
 * 
 * @author Simon Dehne
 *
 */
public class Node implements Comparable<Node> {
	
	private int x;
	private int y;
	private boolean walkable;
	private Node parent;
	private List<Node> children;
	
	// variables for A*
	private int f;			// lowest f is best candidate for next node
	private int g;			// distance to the starting node
	private int h;			// estimated distance to the end node
	
	public Node(int x, int y, boolean walkable) {
		this.x = x;
		this.y = y;
		this.walkable = walkable;
		this.children = new ArrayList<Node>();
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public boolean isWalkable() {
		return walkable;
	}
	
	public Node getParent() {
		return parent;
	}
	
	public List<Node> getChildren() {
		return children;
	}
	
	public int getF() {
		return f;
	}
	
	public int getG() {
		return g;
	}
	
	public int getH() {
		return h;
	}
	
	public void setX(int x) {
		this.x = x;
	}
	
	public void setY(int y) {
		this.y = y;
	}
	
	public void toggleWalkable() {
		walkable = !walkable;
	}
	
	public void setParent(Node parent) {
		this.parent = parent;
	}
	
	public void addChild(Node child) {
		children.add(child);
	}
	
	public void setF(int f) {
		this.f = f;
	}
	
	public void setG(int g) {
		this.g = g;
	}
	
	public void setH(int h) {
		this.h = h;
	}
	
	public String toString() {
		return "("+x+" "+y+")";
	}
	
	// Compare by f value
	@Override
	public int compareTo(Node node) {
		return ((Integer)f).compareTo((Integer)node.getF());
	}

}
